package com.lovo.sys.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lovo.commom.vo.ResultVO;
import com.lovo.sys.bean.ProductBean;
import com.lovo.sys.bean.PromotionalSchemesBean;
import com.lovo.sys.service.IPromotionalSchemesService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 促销controller
 */
@RestController
@RequestMapping("promotional")
public class PromotionalSchemesController {
    @Autowired
    private IPromotionalSchemesService promotionalSchemesService;
    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 查询所有已上架商品
     *
     * @param pageNum 页码
     * @return 分页对象集合
     */
    @RequestMapping("findProductAll")
    public IPage<ProductBean> findProductAll(Integer pageNum) {
        return promotionalSchemesService.findProductAll(pageNum);
    }

    @RequestMapping("findByPromotion")
    public List<PromotionalSchemesBean> findByPromotion() {
        return promotionalSchemesService.findByPromotionalScheme();
    }

    @RequestMapping("addPromotionalProduct")
    public ResultVO addPromotionalProduct(PromotionalSchemesBean promotionalSchemesBean) {
        ResultVO rs = new ResultVO();
        if (promotionalSchemesBean != null) {
            //设置促销订单编号
            long num = System.currentTimeMillis();
            promotionalSchemesBean.setNumber(String.valueOf(num));
            //在数据库中添加促销订单
            promotionalSchemesService.addPromotional(promotionalSchemesBean);

            //添加商品和促销方案中间表
            promotionalSchemesService.addProductByPromotional(promotionalSchemesBean.getRate(), promotionalSchemesBean.getProductIdArr(),
                    promotionalSchemesBean.getPromotionalId());

            //根据商品id查询促销订单
            List<String> productName = promotionalSchemesService.findByProductId(promotionalSchemesBean.getProductIdArr());
            //把商品set进去
            promotionalSchemesBean.setProductName(productName);
            //把促销订单转化为JSON格式，发送到MQ
            String json = JSON.toJSONString(promotionalSchemesBean);
            rabbitTemplate.convertAndSend("directPromotionalExchange", "rabbit.promotional", json);
            rs.setJson("发送成功");
            rs.setTag(0);
            return rs;
        }
        rs.setJson("发送失败");
        rs.setTag(1);
        return rs;
    }

    //    * 根据id删除促销方案信息
//     * 1、先修改促销方案表状态
//     * 2、删除促销方案表和商品表的中间表
//     */
    @RequestMapping("delPromotionalByProduct")
    public ResultVO delPromotionalByProduct(long promotionalId) {
        ResultVO rs = new ResultVO();
        if (promotionalId != 0) {
            promotionalSchemesService.delPromotional(promotionalId);
            rs.setTag(0);
            rs.setJson("删除成功");
        } else {
            rs.setTag(1);
            rs.setJson("删除失败");
        }
        return rs;
    }

    /**
     * 远程接口
     * 根据促销方案编号修改审核状态
     * @param number
     * @param status
     * @return
     */
    @RequestMapping("updateStatus")
    public boolean updateStatus(@RequestParam("number") String number,@RequestParam("status") int status) {
        promotionalSchemesService.updateStatus(number, status);
        return true;
    }
}
